package com.woniuxy.service.impl;

import com.woniuxy.dao.InspectDao;
import com.woniuxy.dao.InspectItemDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.InspectItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service
public class InspectItemServiceImpl implements InspectItemService {
    @Autowired
    InspectItemDao inspectItemDao;
    @Autowired
    InspectDao inspectDao;
    @Override
    public String addInspectItems(InspectItem inspectItem) {

        int num = inspectItem.getNum();
        BigDecimal price = inspectItem.getPrice();
//        inspectItemDao.insertInspectItems(inspectItem);
        //已经有该订单则修改数量，没有则新增，需要用条目主键
        InspectItem findInspectItem = inspectItemDao.selectInspectItemByNoAndId(inspectItem.getPrescriptionNo(), inspectItem.getInspectId());
        if(findInspectItem==null){
            inspectItem.setInspectItemTotalPrice(price.multiply(new BigDecimal(num)));
            log.debug("添加了检查条目，总金额是" + price.multiply(new BigDecimal(num)));
            inspectItemDao.insertInspectItems(inspectItem);
            log.debug("新添加一条检查处方");
            return null;
        }else{
            //inspectItem.setInspectItemTotalPrice(price.multiply(new BigDecimal(findInspectItem.getNum()+num)));
            //inspectItemDao.updataInspectItemNumById(findInspectItem.getInspectItemId(),inspectItem.getNum(),inspectItem.getInspectItemTotalPrice());
            //log.debug("修改检查处方中检查数量");
            return "存在";
        }

    }

    @Override
    public void deleteInspectItems(InspectItem inspectItem) {
        log.debug("删除的检查处方id是" + inspectItem.getInspectItemId());
        inspectItemDao.deleteInspectItem(inspectItem.getInspectItemId());
    }

    @Override
    public PageBean<InspectItem> getAllInspectItemByPrescriptionNo
            (String prescriptionNo,
             int pageIndex,
             int pageSize) {
        List<InspectItem> inspectItems = inspectItemDao.selectInspectItemsByNo(prescriptionNo, pageIndex, pageSize);
        Inspect inspect = new Inspect();
        for(InspectItem inspectItem:inspectItems){
            System.out.println("--------------------------------------------------------");
            inspect.setInspectId(inspectItem.getInspectId());
            List<Inspect> inspects = inspectDao.selectInspectsByCondition(inspect, 1, 1);
            log.info("查询出的检查项目是"+inspects.get(0));
            inspectItem.setInspect(inspects.get(0));
        }

        PageBean<InspectItem> pageBean = new PageBean<>();
        pageBean.setBeanList(inspectItems);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        int totalRecord = inspectItemDao.getInspectItemNum(prescriptionNo);
        pageBean.setTotalRecord(totalRecord);
        return pageBean;
    }

    @Override
    public void modifyInspectItemResult(InspectItem inspectItem) {
        log.debug("修改的检查处方号是"+inspectItem.getInspectItemId());
        inspectItemDao.updateInspectItem(inspectItem.getCheckResult(),inspectItem.getInspectItemId());
    }
}
